﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Xml.Linq;
using System.IO;

namespace Mindfor.Web
{
	/// <summary>
	/// Sends XDocument to output content.
	/// </summary>
	public class XResult : FileResult
	{
		/// <summary>
		/// Gets or sets response XDocument.
		/// </summary>
		public XDocument Document { get; set; }

		/// <summary>
		/// Gets or sets xml save options.
		/// </summary>
		public SaveOptions SaveOptions { get; set; }

		/// <summary>
		/// Initializes new instance.
		/// </summary>
		/// <param name="document">Document to send to response.</param>
		public XResult(XDocument document)
			: base("text/xml")
		{
			if (document == null)
				throw new ArgumentNullException("document");
			Document = document;
			SaveOptions = SaveOptions.None;
		}

		/// <summary>
		/// Initializes new instance.
		/// </summary>
		/// <param name="rootElement">Root element of response document.</param>
		public XResult(XElement rootElement)
			: base("text/xml")
		{
			if (rootElement == null)
				throw new ArgumentNullException("rootElement");
			Document = new XDocument(rootElement);
			SaveOptions = SaveOptions.None;
		}

		protected override void WriteFile(System.Web.HttpResponseBase response)
		{
			Document.Save(response.OutputStream, SaveOptions);
		}
	}
}
